package com.xuyiwei.mayi.queue;

import com.alibaba.rocketmq.client.consumer.DefaultMQPullConsumer;
import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * Created by xuyiwei on 2019/4/22.
 */
public class Customer {
    public static void main(String[] args) throws MQClientException {
        //rmq-group 这个是集群分组，一般都用于集群分组接受消息 对生产者没影响 但是对消费者而言，如果集群的分组一样  则会分摊订阅的topic下的消息，如果不一样则起到广播的效果
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer ("rmq-group");
        consumer.setNamesrvAddr("192.168.66.171:9876;192.168.66.172:9876");
        consumer.setInstanceName("custom");
        consumer.subscribe("itmayiedu-topic", "TagA");
        // *表示订阅itmayiedu-topic这个主题下面的所有分值
        //        consumer.subscribe("itmayiedu-topic", "*");

        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println(msg.getMsgId()+"---"+new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("Consumer Started.");

    }
}
